package com.chris.utils;


import java.util.List;

public class Page {
    private final int totalNum;//总数据条数
    private final int pageSize=10;//每一页的条数
    private  int currentPage;//当前页数
    private final List ls;
    private boolean isAll = false;
    public Page(String currentPage,List ls) throws NumberFormatException,NullPointerException,IllegalArgumentException{
        if(currentPage!=null) {
            this.currentPage = Integer.parseInt(currentPage);
            if (this.currentPage < 1) {
//            System.out.println(currentPage);
                throw new IllegalArgumentException();
            }
        }else isAll = true;
        this.totalNum = ls.size();
        this.ls = ls;
    }
    public List pageNation() throws IllegalArgumentException {
        if(!isAll) {
            int pageNum;//分的页数
            pageNum = (int) Math.ceil(totalNum / (double) pageSize);
            if(totalNum==0) {return ls;};
            System.out.println(pageNum);
            if (currentPage > pageNum) throw new IllegalArgumentException();
            int start = 0;
            start = start + pageSize * (currentPage - 1);
            int end = start + pageSize;
            end = Math.min(end, totalNum);
            return ls.subList(start, end);
        }else return ls;
    }
}
